package com.jiaquyun.jcyx.main.mine

import android.os.Bundle
import android.text.Editable
import android.view.View
import androidx.core.widget.addTextChangedListener
import com.bigkoo.pickerview.builder.OptionsPickerBuilder
import com.bigkoo.pickerview.listener.OnOptionsSelectListener
import com.bigkoo.pickerview.view.OptionsPickerView
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.LogUtils
import com.contrarywind.interfaces.IPickerViewData
import com.jiaquyun.jcyx.R
import com.jiaquyun.jcyx.entity.*
import com.jiaquyun.jcyx.jetpack.mode.AddressViewModel
import com.module.lemlin.owner.OwnerApplication
import com.module.lemlin.owner.OwnerViewModeFragment
import kotlinx.android.synthetic.main.fragment_address_edit.*
import java.lang.StringBuilder


class AddressEditFragment : OwnerViewModeFragment<AddressViewModel>() {

    companion object {
        const val KEY_ADDRESS_EDIT = "KEY_ADDRESS_ACTION"
        fun newInstance(addressDetails: AddressDetails? = null): AddressEditFragment {
            val bundle = Bundle().apply {
                putSerializable(KEY_ADDRESS_EDIT, addressDetails)
            }
            return AddressEditFragment().apply {
                arguments = bundle
            }
        }
    }


    private val mAddressDetails: AddressDetails? by lazy {
        arguments?.getSerializable(KEY_ADDRESS_EDIT) as? AddressDetails
    }

    private val mAddressSave: AddressSave by lazy {
        AddressSave().apply { gender = "1" }
    }

    private val mOptionsPickerView: OptionsPickerView<IPickerViewData> by lazy {
        OptionsPickerBuilder(requireContext()) { options1, options2, options3, _ ->
            val province = viewModel.mPickerData.provinceUIList[options1]
            val city = province.cityList[options2]
            val area = city.areaList[options3]
            val text = StringBuilder().append(province.provinceName)
                .append(city.cityName).append(area.areaName)
            tvAddressEditCity.text = text.toString()
        }.build()
    }

    override fun viewModel(): AddressViewModel {
        return createViewModel(requireActivity())
    }

    override val layoutResId: Int
        get() = R.layout.fragment_address_edit

    override fun initView() {
        mAddressDetails?.let {
            fillData(it)
        }

        tvAddressEditSubmit.isEnabled = isEnabledSubmit()

        //联系人姓名文本修改监听
        etAddressEditName.addTextChangedListener {
            mAddressSave.apply {
                this.consignee = it?.toString()
            }
            tvAddressEditSubmit.isEnabled = isEnabledSubmit()
        }
        //手机号文本修改监听
        etAddressEditPhone.addTextChangedListener {
            mAddressSave.apply {
                this.mobile = it?.toString()
            }
            tvAddressEditSubmit.isEnabled = isEnabledSubmit()
        }
        //地址详情文本修改监听
        etAddressEditDetails.addTextChangedListener {
            mAddressSave.apply {
                this.address = it?.toString()
            }
            tvAddressEditSubmit.isEnabled = isEnabledSubmit()
        }
        //省市区文本修改监听
        tvAddressEditCity.addTextChangedListener {
            mAddressSave.apply {
                this.addressIndexes = it?.toString()
            }
            tvAddressEditSubmit.isEnabled = isEnabledSubmit()
        }
        //默认地址选中监听
        cbAddressEditDefault.setOnCheckedChangeListener { _, isChecked ->
            mAddressSave.apply {
                this.is_default = if (isChecked) "1" else null
            }
        }

        //“先生”称呼选中监听
        rbAddressEditMale.setOnCheckedChangeListener { _, isChecked ->
            if (!isChecked)
                return@setOnCheckedChangeListener
            mAddressSave.apply {
                this.gender = "1"
            }
        }
        //“女士”称呼选中监听
        rbAddressEditGirl.setOnCheckedChangeListener { _, isChecked ->
            if (!isChecked)
                return@setOnCheckedChangeListener
            mAddressSave.apply {
                this.gender = "2"
            }
        }

        tvAddressEditCity.setOnClickListener {
            mOptionsPickerView.setPicker(
                viewModel.mPickerData.provinceUIList,
                viewModel.mPickerData.cityUIList,
                viewModel.mPickerData.areaUIList
            )
            mOptionsPickerView.show()
        }

        tvAddressEditSubmit.setOnClickListener {
            viewModel.userAddressSave(mAddressSave)
        }

        tvAddressEditDelete.setOnClickListener {
            val id = mAddressDetails?.id
                ?: return@setOnClickListener
            viewModel.userAddressDelete(id)
        }
    }

    override fun initData() {

    }

    private fun fillData(address: AddressDetails) {

        etAddressEditName.text = Editable.Factory.getInstance()
            .newEditable(address.consignee)
        etAddressEditPhone.text = Editable.Factory.getInstance()
            .newEditable(address.mobile)
        tvAddressEditCity.text = address.area
        etAddressEditDetails.text = Editable.Factory.getInstance()
            .newEditable(address.address)

        if (address.gender == 1) {
            rbAddressEditMale.isChecked = true
        } else {
            rbAddressEditGirl.isChecked = true
        }
        cbAddressEditDefault.isChecked = address.is_default

        tvAddressEditDelete.visibility = View.VISIBLE

        mAddressSave.apply {
            this.id = address.id.toString()
            this.consignee = address.consignee
            this.gender = address.gender.toString()
            this.mobile = address.mobile
            this.addressIndexes = address.area
            this.address = address.address
            this.is_default = if (address.is_default) "1" else null
        }
    }

    private fun isEnabledSubmit(): Boolean {
        return !etAddressEditName.text.isNullOrEmpty()
                && !etAddressEditPhone.text.isNullOrEmpty()
                && !etAddressEditDetails.text.isNullOrEmpty()
                && !tvAddressEditCity.text.isNullOrEmpty()
                && (rbAddressEditMale.isChecked || rbAddressEditGirl.isChecked)
    }
}

